//
//  MCQiPaizhuanzhangHeaderView.m
//  TLYL
//
//  Created by MC on 2017/10/25.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCQiPaizhuanzhangHeaderView.h"
#import "MCUserMoneyModel.h"
#import "MCGetGameBalanceModel.h"
#import "MCGetVrBalanceModel.h"
#import "MCMineInfoModel.h"

@interface MCQiPaizhuanzhangHeaderView ()

@property (nonatomic,strong)MCUserMoneyModel * userMoneyModel;
@property (nonatomic,strong)MCGetGameBalanceModel *getGameBalanceModel;
@property (nonatomic,strong)MCGetVrBalanceModel *getVrBalanceModel;
@property (nonatomic,strong)UILabel * lab3;

@property (nonatomic,strong)UILabel * lab2;


@property (nonatomic,assign)int T;

@end

@implementation MCQiPaizhuanzhangHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor=RGB(249, 249, 249);
    
    CGFloat W = G_SCREENWIDTH-54;
    CGFloat L = 50;
    CGFloat H = 30;
    
    UILabel * lab1=[[UILabel alloc]init];
    [self setLab:lab1 withColor:RGB(0, 0, 0) andFont:11 andText:@"彩票余额" andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.height.mas_equalTo(H);
        make.width.mas_equalTo(L);
        make.left.equalTo(self.mas_left).offset(13);
    }];
    
    UILabel * lottory_yuElab=[[UILabel alloc]init];
    [self setLab:lottory_yuElab withColor:RGB(249,84,83) andFont:11 andText:@"加载中..." andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:lottory_yuElab];
    _lottory_yuElab=lottory_yuElab;
    [lottory_yuElab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.height.mas_equalTo(H);
        make.left.equalTo(lab1.mas_right).offset(0);
        make.width.mas_equalTo(W/2.0-L);
    }];
    
    
    
    
    
    UILabel * lab2=[[UILabel alloc]init];
    [self setLab:lab2 withColor:RGB(0, 0, 0) andFont:11 andText:@"棋牌余额" andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:lab2];
    _lab2=lab2;
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.height.mas_equalTo(H);
        make.width.mas_equalTo(L);
        make.left.equalTo(self.mas_left).offset(W/2.0);
    }];
    
    UILabel * qipai_yuElab=[[UILabel alloc]init];
    [self setLab:qipai_yuElab withColor:RGB(249,84,83) andFont:11 andText:@"加载中..." andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:qipai_yuElab];
    _qipai_yuElab=qipai_yuElab;
    [qipai_yuElab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(lab2.mas_right).offset(0);
        make.width.mas_equalTo(W/2.0-L);
        make.height.mas_equalTo(H);
    }];
    
    
    UILabel * lab3=[[UILabel alloc]init];
    [self setLab:lab3 withColor:RGB(0, 0, 0) andFont:11 andText:@"真人余额" andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:lab3];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(H);
        make.width.mas_equalTo(L);
        make.height.mas_equalTo(H);
        make.left.equalTo(self.mas_left).offset(13);
    }];
    _lab3=lab3;
    
    UILabel * person_yuElab=[[UILabel alloc]init];
    [self setLab:person_yuElab withColor:RGB(249,84,83) andFont:11 andText:@"加载中..." andTextAlignment:NSTextAlignmentLeft];
    [self addSubview:person_yuElab];
    _person_yuElab=person_yuElab;
    [person_yuElab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(H);
        make.left.equalTo(lab3.mas_right).offset(0);
        make.width.mas_equalTo(W/2.0-L);
        make.height.mas_equalTo(H);
    }];
    
    
    NSString *sMode = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:MerchantMode]];
    
    int Mode = [sMode intValue];

    if (((Mode & 32) != 32) && ((Mode & 16) != 16)) {
        
    }else if(((Mode & 16) == 16) && ((Mode & 32) == 32)){
        //显示真人+棋牌
        _lab3.hidden=NO;
        _person_yuElab.hidden=NO;
        
        _lab2.hidden=NO;
        _qipai_yuElab.hidden=NO;
        
    }else if (((Mode & 16) != 16) && ((Mode & 32) == 32)){
        //显示棋牌
        //隐藏真人
        _lab3.hidden=YES;
        _person_yuElab.hidden=YES;
        
        _lab2.hidden=NO;
        _qipai_yuElab.hidden=NO;
        
    }else if(((Mode & 16) == 16) && ((Mode & 32) != 32)){
        //显示真人
        //隐藏棋牌
        _lab3.hidden=NO;
        _person_yuElab.hidden=NO;
        
        _lab2.hidden=YES;
        _qipai_yuElab.hidden=YES;
        
    }
    
    
    
    UIView * line = [[UIView alloc]init];
    line.backgroundColor=RGB(231,231,231);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(self);
        make.left.equalTo(self.mas_left).offset(G_SCREENWIDTH-54);
        make.width.mas_equalTo(0.5);
    }];
    
    
    UIImageView * yuEimgV=[[UIImageView alloc]init];
    [self addSubview:yuEimgV];
    yuEimgV.frame=CGRectMake(G_SCREENWIDTH-19-16, 22, 16, 16);
    yuEimgV.image=[UIImage imageNamed:@"shuaxinyue"];
    yuEimgV.userInteractionEnabled=YES;
    
    // 单击的 Recognizer
    UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(shuaXin)];
    //点击的次数
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [yuEimgV addGestureRecognizer:singleRecognizer];
    
    
    
}

-(void)shuaXin{
    
    if (self.shuaXinBlock) {
        self.shuaXinBlock();
    }
    
    
    
}

-(void)shuaXinYuE:(QiPaiCompeletion)compeletion{
    __weak __typeof__ (self) wself = self;
    
    int A=1;
    NSString *sMode = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:MerchantMode]];
    int Mode = [sMode intValue];
    MCMineInfoModel *mineInfoModel =[MCMineInfoModel sharedMCMineInfoModel];
    int IsTestAccount = [[NSString stringWithFormat:@"%@",mineInfoModel.UserType] intValue];
    
    //注： IsTestAccount 代表是否为试玩账号：0为正式账户，1为试玩账户
    if ((Mode & 16) != 16 || IsTestAccount == 1) {
        //隐藏真人
        A=1;
    }else{
        A=2;
    }
    
    _T = 0;
    
    //刷新余额
    MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    [userMoneyModel refreashDataAndShow];
    self.userMoneyModel=userMoneyModel;
    //    dispatch_group_t group = dispatch_group_create();
    //    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    //    dispatch_group_async(group, quene, ^{
    userMoneyModel.callBackSuccessBlock = ^(id manager) {
        wself.userMoneyModel.FreezeMoney=manager[@"FreezeMoney"];
        wself.userMoneyModel.LotteryMoney=manager[@"LotteryMoney"];
        wself.lottory_yuElab.text = manager[@"LotteryMoney"];
        
        NSLog(@"complete task 1-YES");
        //                dispatch_semaphore_signal(semaphore);
        wself.T++;
        if (wself.T>A) {
            compeletion(YES);
        }
    };
    
    userMoneyModel.callBackFailedBlock = ^(id manager, NSString *errorCode) {
        
        wself.T++;
        if (wself.T>A) {
            compeletion(YES);
        }
        NSLog(@"complete task 1-NO");
        //                dispatch_semaphore_signal(semaphore);
        
    };
    //        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    //    });
    
    
    MCGetGameBalanceModel *getGameBalanceModel = [MCGetGameBalanceModel sharedMCGetGameBalanceModel];
    [getGameBalanceModel refreashDataAndShow];
    self.getGameBalanceModel=getGameBalanceModel;
    
    //    dispatch_group_async(group, quene, ^{
    //
    getGameBalanceModel.callBackSuccessBlock = ^(id manager) {
        wself.getGameBalanceModel.DsBalance=manager[@"DsBalance"];
        wself.qipai_yuElab.text = manager[@"DsBalance"];
        if (wself.qipai_yuElab.text.length<1) {
            wself.qipai_yuElab.text = @"0";
        }
        //            dispatch_semaphore_signal(semaphore);
        NSLog(@"complete task 2-YES");
        wself.T++;
        if (wself.T>A) {
            compeletion(YES);
        }
    };
    
    getGameBalanceModel.callBackFailedBlock = ^(id manager, NSString *errorCode) {
        wself.T++;
        //            dispatch_semaphore_signal(semaphore);
        NSLog(@"complete task 2-NO");
        if (wself.T>A) {
            compeletion(YES);
        }
    };
    
    
    //        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    //    });
    
    
    MCGetVrBalanceModel *getVrBalanceModel = [MCGetVrBalanceModel sharedMCGetVrBalanceModel];
    [getVrBalanceModel refreashDataAndShow];
    self.getVrBalanceModel=getVrBalanceModel;
    
    //    dispatch_group_async(group, quene, ^{
    //
    getVrBalanceModel.callBackSuccessBlock = ^(id manager) {
        if (manager== nil || [manager isKindOfClass:[NSNull class]]) {
            wself.person_yuElab.text = @"0";
        }
        NSString *DsBalance=[NSString stringWithFormat:@"%@", manager[@"Balance"]];
        if (DsBalance.length<1||[DsBalance isEqualToString:@"(null)"]) {
            DsBalance=@"0";
        }
        wself.getGameBalanceModel.DsBalance=DsBalance;
        wself.person_yuElab.text =GetRealSNum(DsBalance);
        
        //            dispatch_semaphore_signal(semaphore);
        NSLog(@"complete task 3-YES");
        wself.T++;
        if (wself.T>A) {
            compeletion(YES);
        }
    };
    
    getVrBalanceModel.callBackFailedBlock = ^(id manager, NSString *errorCode) {
        wself.T++;
        //            dispatch_semaphore_signal(semaphore);
        NSLog(@"complete task 3-NO");
        if (wself.T>A) {
            compeletion(YES);
        }
    };
    
    
    //        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    //    });
    
    
    //    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    NSLog(@"完成了网络请求，不管网络请求失败了还是成功了。");
    
    //        if (wself.T>1) {
    //            NSLog(@"11111111111111111111111111111111111");
    //            compeletion(YES);
    //        }else{
    //            NSLog(@"22222222222222222222222222222222222");
    //            compeletion(NO);
    //        }
    //    });
    
    
}


-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}



+(CGFloat)computeHeight:(id)info{
    return 60;
}

-(void)setDataSource:(id)dataSource{
    
    _dataSource=dataSource;
    
    
    
    
}

//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    label.attributedText = str;
}


@end





























